package org.csu.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.mypetstore.commonResponse.CommonResponse;
import org.csu.mypetstore.domin.Account;
import org.csu.mypetstore.persistence.AccountMapper;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;


import java.util.Random;

@Service("mailService")
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender mailSender;//一定要用@Autowired

    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountMapper accountMapper;

    //application.properties中已配置的值
    @Value("${spring.mail.username}")
    private String from;


    public CommonResponse<String> sendMimeMail(String email, HttpSession session) {

            SimpleMailMessage mailMessage = new SimpleMailMessage();

            mailMessage.setSubject("JPetStore邮箱验证码");//主题
            //生成随机数
            String code = randomCode();

            //将随机数放置到session中
            session.setAttribute("email",email);
            session.setAttribute("EmailCode",code);

            mailMessage.setText("【CSU软工四班小组科技】验证码"+code+" 用于QQ邮箱身份验证，1分钟内有效，请勿泄露和转发。如非本人操作，请忽略此短信。");//内容

            mailMessage.setTo(email);//发给谁

            mailMessage.setFrom(from);//你自己的邮箱

            mailSender.send(mailMessage);//发送
            return  CommonResponse.createForSuccess("发送成功");
    }

    /**
     * 随机生成6位数的验证码
     * @return String code
     */
    public String randomCode(){
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }


    public CommonResponse<Account> loginIn(String email, String code,HttpSession session){
        //获取session中的验证信息
        String email1 = (String) session.getAttribute("email");
        String code1 = (String) session.getAttribute("EmailCode");

        if(!email.equals(email1)){
            return CommonResponse.createForError("邮箱不匹配");
        }else if(!code.equals(code1)){
            return CommonResponse.createForError("验证码不匹配");
        }
        CommonResponse<Account> commonResponse = accountService.getAccountByEmail(email);
        if(commonResponse.getStatus()==0){
            return CommonResponse.createForError(commonResponse.getMessage());
        }
        return CommonResponse.createForSuccess("匹配成功",commonResponse.getData());
    }

    @Override
    public CommonResponse<String> addEmail(String email, String username) {
        UpdateWrapper<Account> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("userid",username);
        queryWrapper.set("email",email);
        int res = accountMapper.update(null,queryWrapper);
        if(res == 0){
            return CommonResponse.createForError("更新失败");
        }
        return CommonResponse.createForSuccessMessage("更新成功");
    }
}
